var gulp = require("gulp");
var gulp_clean_css = require("gulp-clean-css");//压缩css代码
var gulp_sass = require("gulp-sass");
var gulp_mini_js = require("gulp-uglify");//压缩js
var gulp_tinypng = require("gulp-tinypng");
var gulp_autoprefixer = require ("gulp-autoprefixer");//添加前缀
var browser_sync = require("browser-sync").create();//开启监听
var gulp_plumber = require("gulp-plumber");//防止错误跳出命令

//gulp.task("test",function(){
//	console.log("这是个测试");
//})
//创建实时监听
gulp.task("dev",function(){
	browser_sync.init({
		port:1234,
		server:{
			baseDir:"./src"	
		}
	})
	gulp.watch("src/scss/**/*.scss",["minscss"]).on('change',browser_sync.reload)
	gulp.watch("src/js/**/*.js").on('change',browser_sync.reload)
	gulp.watch("src/**/*.html").on('change',browser_sync.reload)
})
//编译sass的任务
gulp.task("miniscss",function(){
	gulp.src("src/scss/**/*.scss")
		.pipe(gulp_plumber())
	    .pipe(gulp_sass())
	    .pipe(gulp_plumber())
	    .pipe(gulp.dest("src/css/"))
})
//压缩css的任务
gulp.task("minicss",function(){
	gulp.src("src/css/**/*.css")
	    .pipe(gulp_autoprefixer())
	    .pipe(gulp_plumber())
	    .pipe(gulp_clean_css())
	    .pipe(gulp.dest("dist/css"))
})
//压缩js的任务
gulp.task("minijs",function(){
	gulp.src("src/js/**/*.js")
	    .pipe(gulp_plumber())
	    .pipe(gulp_mini_js())
	    .pipe(gulp.dest("dist/js"))
})
//压缩图片的任务
gulp.task("tinypng",function(){
	gulp.src("src/img/**/*.{png,jpg}")
	    .pipe(gulp_plumber())
	    .pipe(gulp_tinypng('8xUS5CwNCUPDIqvpWg6eIbvJGI05a6h0'))
	    .pipe(gulp.dest("dist/img"))
})
//复制html到dist目录下
gulp.task("copy",function(){
	gulp.src("src/**/*.html")
	    .pipe(gulp.dest("dist"))
})
//一键打包
gulp.task("pro",["minicss","minijs","tinypng","copy"]);